﻿using System;
using System.Diagnostics;
using Smarly.Idml.RelaxNg.Generator.Model;
using Tenuto.Grammar;

namespace Smarly.Idml.RelaxNg.Generator.Proxy
{
	public class AttributeExpBuilderBase : NamedBuilder<AttributeExp>
	{
		[DebuggerStepThrough]
		protected internal AttributeExpBuilderBase(AttributeExp exp, Action<IObjectName> updateName) : base(exp, updateName) { }

		public override IStruct Fill(IStruct getStruct = null)
		{
			getStruct = base.Fill(getStruct);

			if (getStruct is ObjectStruct)
			{
				var obj = (ObjectStruct) getStruct;
				AttrStruct attrStruct = new AttrStruct
					                        {
						                        Name = Name, 
																		IsNull = IsNull,
					                        };
				Factory(Exp.exp, UpdateName).Fill(attrStruct);
				obj.Structs.Add(attrStruct);
			}
			else if (getStruct is AttrStruct)
			{
				AttrStruct attrStruct = (AttrStruct) getStruct;
				attrStruct.Name = Name;
				attrStruct.IsNull = IsNull;
				Factory(Exp.exp, UpdateName).Fill(attrStruct);
			}
			else
			{
				throw new NotImplementedException();
			}

			return getStruct;
		}

		public override IStruct CreateIStruct()
		{
			return new AttrStruct();
		}
	}
}